﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Remediation.net.bing.api;

namespace Remediation
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class RemediationService : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }

        public string[] GetRemediation(string[] topicTags)
        {
            //set up the services and the remediation URLs array
            BingService bservice = new BingService();
            string[] remediationResultsArray = new string[10 * topicTags.Length];
            int remediationResultsArrayLength = 0;
            uint maxSearchResults = 10;
            IEnumerable<string> inputList = topicTags;
    
            //find which search term appears the most; we'll search for that one
            //if it's a tie, the first appearing term is selected

            //taken from Stack Overflow with slight modifications
            var mostFrequentlyUsed = inputList.GroupBy(word => word).Select(wordGroup => new { Word = wordGroup.Key, Frequency = wordGroup.Count() }).OrderByDescending(word => word.Frequency);
                        
            //complete the web search
            SearchRequest request = new SearchRequest();

            request.AppId = "C92824F2D31A2EFCFC3AEC31168E9DBC5AC2DA8D";
            request.Query = mostFrequentlyUsed.First().Word.ToString();
            request.Sources = new SourceType[] { SourceType.Web };

            request.Web = new WebRequest();
            request.Web.Count = maxSearchResults;
            request.Web.CountSpecified = true;

            SearchResponse response = bservice.Search(request);

            if (request.Query != "" && request.Query != null)
            {
                //convert the web search results into WSDL addresses
                for (int i = 0; i < maxSearchResults; i++)
                {
                    remediationResultsArray[remediationResultsArrayLength] = response.Web.Results[i].Url;
                    remediationResultsArrayLength++;
                }
            }
            else
            {
                remediationResultsArray[0] = "Search failed in part or whole.";
            }

        //send the aggregate remediation URL addresses array on its way
        return remediationResultsArray;
        }
    }
}
